package com.qkjia.day02;

import com.qkjia.day01.SleepUtils;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Test03_ReentrantLock {
	private Lock lock = new ReentrantLock();
	//通知队列
	private Condition condition = lock.newCondition();
	//
	private ReentrantReadWriteLock lock1=new ReentrantReadWriteLock();

	public void method1() throws Exception {
		lock.lock();
		System.out.println("当前线程" + Thread.currentThread().getName() + "进入等待状态");
		SleepUtils.second(3);
		System.out.println("当前线程" + Thread.currentThread().getName() + "即将释放锁");
		condition.await();// Object
		System.out.println("当前线程" + Thread.currentThread().getName() + "继续执行");
		lock.unlock();
	}

	public void method2()  {
		lock.lock();
		System.out.println("当前线程" + Thread.currentThread().getName() + "进入等待状态...");
		SleepUtils.second(3);
		System.out.println("当前线程" + Thread.currentThread().getName() + "发出唤醒...");
		condition.signal();
		lock.unlock();
	}

	public static void main(String[] args) {
		final Test03_ReentrantLock testLock = new Test03_ReentrantLock();
		new Thread(()->{
			try {
				testLock.method1();
			} catch (Exception e) {
			}
		},"AA").start();

		new Thread(()->{
			testLock.method2();
		},"BB").start();
	}
}